import { LOGIN_QUIT, LOGIN_SUCCESS, UserAction } from "./userActions"
import { l } from '../../utils'
const initState = {
	username: '',
	nickname: '',
	avator: '',
	token: l.getItem('token') || '',
	role: '',
}

// 获取 initState 类型
export type UserState = typeof initState

const userReducer = (state = initState, action: UserAction) => {
	switch (action.type) {
		// 成功登录
		case LOGIN_SUCCESS: {
			return {
				...state,
				...action.payload
			}
		}
		// 退出登录
		case LOGIN_QUIT: {
			return {
				username: '',
				nickname: '',
				avator: '',
				token: '',
				role: '',
			}
		}
		default: return state
	}
}

export default userReducer